//package com.ly.mpayment.rest.system.ele;
//
//import java.math.BigDecimal;
//import java.util.Map;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.ws.rs.POST;
//import javax.ws.rs.Path;
//import javax.ws.rs.core.Context;
//
//import com.ly.mpayment.rest.recharge.RechargeRest;
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//import org.springframework.stereotype.Component;
//
//import com.ly.mpayment.core.util.BigUtil;
//import com.ly.mpayment.core.util.Config;
//import com.ly.mpayment.core.util.DateUtils;
//import com.ly.mpayment.core.util.JsonUtils;
//import com.ly.mpayment.core.util.MapUtil;
//import com.ly.mpayment.core.util.ResultMessage;
//import com.ly.mpayment.persist.entity.UpOrder;
//import com.ly.mpayment.persist.entity.UpShopInfo;
//import com.ly.mpayment.persist.entity.ele.ElectronicAccount;
//import com.ly.mpayment.persist.service.downrechargeflow.IDownRechargeFlowService;
//import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
//import com.ly.mpayment.persist.service.ele.IElectronicAccountService;
//import com.ly.mpayment.persist.service.uporder.IUporderService;
//import com.ly.mpayment.persist.service.upshopinfo.IUpShopInfoService;
//import com.ly.mpayment.persist.service.user.IUserService;
//import com.ly.mpayment.rest.AbsRest;
//
//import cn.jpush.api.utils.StringUtils;
//import net.sf.json.JSONObject;
//
//@Component
//@Path("system/electronic/")
//public class ElectronicAccountRest extends AbsRest{
//
//	Logger logger = Logger.getLogger(ElectronicAccountRest.class);
//
//	@Autowired
//	IUserService userService;
//
//	@Autowired
//	IElectronicAccountService electronicAccountService;
//
//
//	@Autowired
//	IUporderService upOrderService;
//
//	@Autowired
//	private ThreadPoolTaskExecutor taskExecutor;
//
//	@Autowired
//	IDownRechargeFlowService flowService;
//
//	@Autowired
//	IDownShopInfoService shopService;
//
//	@Autowired
//	IUpShopInfoService upShopInfoService;
//
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("findPageAccount")
//	public String findPageAccount(String data,@Context HttpServletRequest request){// request, userService,
//		String result = this.validationParameter(data,request,userService, "start","pageSize","accountName","phoneNum");
//		if(result == null){
//			JSONObject json = JSONObject.fromObject(data);
//			Map<String, Object> parameters = MapUtil.getMap();
//			if(!StringUtils.isEmpty(json.getString("accountName"))){
//				parameters.put(":accountName", json.getString("accountName"));
//			}
//			if(!StringUtils.isEmpty(json.getString("phoneNum"))){
//				parameters.put(":phoneNum", json.getString("phoneNum"));
//			}
//			return ResultMessage.getSucceed( JsonUtils.excludes(electronicAccountService.findPageElectronic(parameters, json.getInt("start"), json.getInt("pageSize"))
//					, new String[]{"eleRecords",""}));
//		}
//		return result;
//	}
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("saveAccount")
//	public String saveAccount(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request,userService, "accountName","password","payPassword","phoneNum","comName","city");
//		if(result == null){
//			JSONObject json = JSONObject.fromObject(data);
//			ElectronicAccount account = this.electronicAccountService.findByNameAndPhone(json.getString("accountName"));
//			if(account == null){
//				account = new ElectronicAccount();
//				account.setAccountName(json.getString("accountName"));
//				account.setPassword(json.getString("password"));
//				account.setPayPassword(json.getString("payPassword"));
//				account.setPhoneNum(json.getString("phoneNum"));
//				account.setComName(json.getString("comName"));
//				account.setAccstate(Config.publicStatus[0]);
//				account.setCity(json.getString("city"));
//				account.setBalance(BigDecimal.ZERO);
//				account.setCreateTime(DateUtils.getNowDate());
//				if(electronicAccountService.saveElectronicAccount(account)){
//					return ResultMessage.getSucceed(null);
//				}
//				return ResultMessage.getFailt("");
//			}
//			return ResultMessage.getFailt(ResultMessage.eleAccountError);
//		}
//		return result;
//	}
//
//	@POST
//	@Path("updateAccount")
//	@SuppressWarnings("static-access")
//	public String updateAccount(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request,userService,"accountName","password","payPassword","phoneNum","accId","comName","city");
//		if(result == null){
//			JSONObject json = JSONObject.fromObject(data);
//			ElectronicAccount account = this.electronicAccountService.findByid(json.getString("accId"));
//			ElectronicAccount ele = this.electronicAccountService.findByNameAndPhone(json.getString("accountName"));
//			if(account != null && (ele != null && ele.getAccId().equals(account.getAccId()))){
//				account.setAccountName(json.getString("accountName"));
//				account.setPassword(json.getString("password"));
//				account.setPayPassword(json.getString("payPassword"));
//				account.setPhoneNum(json.getString("phoneNum"));
//				account.setComName(json.getString("comName"));
////				account.setAccstate(Config.publicStatus[1]);
//				account.setCreateTime(DateUtils.getNowDate());
//				account.setCity(json.getString("city"));
//				if(electronicAccountService.updateElectronicAccount(account)){
//					return ResultMessage.getSucceed(null);
//				}
//				return ResultMessage.getFailt("");
//			}else if(ele == null && account != null){
//				account.setAccountName(json.getString("accountName"));
//				account.setPassword(json.getString("password"));
//				account.setPayPassword(json.getString("payPassword"));
//				account.setComName(json.getString("comName"));
//				account.setPhoneNum(json.getString("phoneNum"));
//				account.setAccstate(Config.publicStatus[1]);
//				account.setCreateTime(DateUtils.getNowDate());
//				account.setCity(json.getString("city"));
//				if(electronicAccountService.updateElectronicAccount(account)){
//					return ResultMessage.getSucceed(null);
//				}
//				return ResultMessage.getFailt("");
//			}
//			return ResultMessage.getFailt(ResultMessage.eleAccountError);
//		}
//		return result;
//	}
//
//	@POST
//	@Path("updateAccstate")
//	@SuppressWarnings("static-access")
//	public String updateAccstate(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request,userService,"accstate","accId");
//		if(result == null){
//			JSONObject json = JSONObject.fromObject(data);
//			ElectronicAccount account = this.electronicAccountService.findByState(json.getString("accId"));
//			ElectronicAccount old = this.electronicAccountService.findByid(json.getString("accId"));
//			if(account == null ){
//				if(old != null ){
//					old.setAccstate(json.getInt("accstate"));
//					if(electronicAccountService.updateElectronicAccount(old)){
//						return ResultMessage.getSucceed(null);
//					}
//					return ResultMessage.getFailt("");
//				}
//				return ResultMessage.getFailt(ResultMessage.eleAccountError);
//			}else if(old != null && account != null && old.getAccId().equals(account.getAccId())){
//				//禁用时 清空登录信息
////				if(json.getInt("accstate") == 0 && ElectronicUtil.jsonToken != null){
////					ElectronicUtil.jsonToken.clear();
////				}
//				old.setSessionToken("");
//				old.setAccstate(json.getInt("accstate"));
//				ElectronicUtil.jsonToken = new JSONObject();
//				if(electronicAccountService.updateElectronicAccount(old)){
//					return ResultMessage.getSucceed(null);
//				}
//				return ResultMessage.getFailt("");
//			}else{
//				return ResultMessage.getFailt(ResultMessage.chargeAccountError);
//			}
//		}
//		return result;
//	}
//
//	@POST
//	@Path("loginEle")
//	@SuppressWarnings("static-access")
//	public String loginEle(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request,userService,"accountName","password","rand","msgRand","accId");
//		if(result == null){
//			JSONObject json = JSONObject.fromObject(data);
//			ElectronicAccount account = this.electronicAccountService.findByState(json.getString("accId"));
//			if(account == null){
//				return login(account, json);
//			}else{
//				if(account.getAccId().equals(json.getString("accId"))){
//					return login(account, json);
//				}
//				return ResultMessage.getFailt("请先退出其他账号");
//			}
//		}
//		return result;
//	}
//	/**
//	 * 登录
//	 * @param account
//	 * @param json
//	 * @return
//	 */
//	private String login(ElectronicAccount account,JSONObject json){
//		JSONObject login = electronicUtil.login(json.getString("accountName"), json.getString("password"), json.getString("rand"), json.getString("msgRand"),account.getSessionToken());
//		if(login != null && login.containsKey(Config.JSESSIONID)){
//			account = this.electronicAccountService.findByid(json.getString("accId"));
//			if(account != null){
//				String balance = electronicUtil.getRefreshTop(login.getString(Config.JSESSIONID));
//				if(balance != null && !balance.equals("")){
//					account.setBalance(BigUtil.getBigDecimal(balance.replaceAll("\"", "").replaceAll("&nbsp;", "").replaceAll("保证金余额:", "")));
//				}
//				account.setSessionToken(login.getString(Config.JSESSIONID));
//				account.setAccstate(Config.accstate[2]);
//				this.electronicAccountService.updateElectronicAccount(account);
//				return ResultMessage.getSucceed(null);
//			}
//			return ResultMessage.getFailt("");
//		}else if(login != null){
//			return ResultMessage.getFailt(login.toString());
//		}else{
//			return ResultMessage.getFailt("");
//		}
//	}
//
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("getRand")
//	public String getRand(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request,userService,"accId");
//		if(result == null){
//			JSONObject json = JSONObject.fromObject(data);
//			JSONObject modul = electronicUtil.getLoginKey();
//			json.put("modulus", modul.getString("modulus"));
//			logger.info("modulus:"+json.getString("modulus"));
//			ElectronicAccount account = this.electronicAccountService.findByid(json.getString("accId"));
//			if(account != null && modul.containsKey(Config.JSESSIONID)){
//				account.setSessionToken(modul.getString(Config.JSESSIONID));
//				electronicAccountService.updateElectronicAccount(account);
//				String rand = electronicUtil.getCaptchaVal(account.getSessionToken());
//				if(rand != null){
//					json.put("rand", rand);
//					return ResultMessage.getSucceed( json.toString());
//				}
//				return ResultMessage.getFailt("");
//			}
//		}
//		return result;
//	}
//
//	@POST
//	@Path("getMsgRand")
//	@SuppressWarnings("static-access")
//	public String getMsgRand(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request,userService,"accountName","password","rand");
//		if(result == null){
//			JSONObject json = JSONObject.fromObject(data);
//			logger.info(data);
//			ElectronicAccount account = electronicAccountService.findByNameAndPhone(json.getString("account"));
//			if(account != null){
//				result = electronicUtil.getMsgCode(json.getString("accountName"), json.getString("password"), json.getString("rand"),account.getSessionToken());
//				if(result.equals(Config.success)){
//					//登录成功后为false
//					return ResultMessage.getSucceed(null);
//				}else{
//					return ResultMessage.getFailt(result);
//				}
//			}
//		}
//		return result;
//	}
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("getRefreshTop")
//	public String getRefreshTop(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request,userService,"accId");
//		if(result == null){
//			JSONObject par = JSONObject.fromObject(data);
//			ElectronicAccount account = electronicAccountService.findByid(par.getString("accId"));
//			if(account != null && account.getSessionToken() != null &&account.getSessionToken().isEmpty()){
//				return ResultMessage.getFailt(ResultMessage.eleNotLoginError);
//			}
//			try{
//				result = electronicUtil.getRefreshTop(account.getSessionToken());
//				if(result != null && !result.replaceAll("\"", "").isEmpty()){
//					JSONObject json = JsonUtils.getJSONOBJCT();
//					account = electronicAccountService.findByid(par.getString("accId"));
//					if(account != null){
//						account.setBalance(BigUtil.getBigDecimal(result.replaceAll("\"", "").replaceAll("&nbsp;", "").replaceAll("保证金余额:", "")));
//					}
//					this.electronicAccountService.updateElectronicAccount(account);
//					json.put("status", account.getSessionToken() == null || account.getSessionToken().isEmpty() ? 0 : 1);
//					json.put("balance", result.replaceAll("\"", "").replaceAll("&nbsp;", ""));
//					return ResultMessage.getSucceed(json.toString());
//				}else{
//					return ResultMessage.getFailt("登录失效,请重新登录或查询失败");
//				}
//			}catch (Exception e){
//				e.printStackTrace();
//			}
//		}
//		return result;
//	}
//
//
//	@SuppressWarnings("static-access")
//	public void commitElectronic(UpOrder order, UpShopInfo shop) {
//		order = upOrderService.findByEleId(order.getUporderuuid());
//		if (order != null && new BigDecimal(order.getUpShopPro().getUpfacevalue()).compareTo(BigDecimal.ZERO) == 1
//				&& new BigDecimal(order.getUpShopPro().getUpfacevalue()).compareTo(new BigDecimal(5000)) == -1) {
//			//检查号码当天是否存在，如果存在则直接返回错误
//			//检查充值号码的地市是不是当前允许的地市
//			//修改为面值
//			String result = upOrderService.addEle(order,order.getUpShopPro().getUpfacevalue());
//			logger.info("commitElectronic_result:"+result+","+order.getUpphonenum());
//			if(result != null){
//				JSONObject json = JSONObject.fromObject(result);
//				if(json.getString("code").equals(ResultMessage.succeed.getCode()) && json.containsKey("data") && !json.getString("data").isEmpty()){
//					upOrderService.updateUpOrder(order, json.getJSONObject("data").getString("recordId"));
//				}else if(json.getString("code").equals(ResultMessage.chargeEleError.getCode()) || json.getString("code").equals(ResultMessage.chargeAmountError.getCode())){
//					RechargeRest.failtUpOrder.put(order.getUporderuuid(), json.getString("message"));
//					flowService.orderHanlderFailt(order, upOrderService, flowService, shopService, taskExecutor,json.getString("message"));
//				}
//			}
//		}else{
//			logger.info("commitElectronic error not found product" + order.getUporderuuid());
//		}
//	}
//
//
////	@POST
////	@Path("logout")
////	public String logout(String data,@Context HttpServletRequest request){
////		String result = this.validationParameter(data,request,userService,"");
////		if(result == null){
////			if(ElectronicUtil.jsonToken == null || ElectronicUtil.jsonToken.isEmpty()){
////				return ResultMessage.getFailt(ResultMessage.eleNotLoginError);
////			}
////			ElectronicAccount account = this.electronicAccountService.findByState();
////			if(account != null){
////				account.setAccstate(Config.accstate[1]);
////				this.electronicAccountService.updateElectronicAccount(account);
////				ElectronicUtil.jsonToken.clear();
////				return ResultMessage.getSucceed(null);
////			}
////			return ResultMessage.getFailt("");
////		}
////		return ResultMessage.getFailt("");
////	}
//
//
//}
